package com.sunfeng.netty.atguigu.http;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import lombok.extern.slf4j.Slf4j;


/**
 * @author 孙峰
 * @Description: TODO
 * @date 2020/12/3 10:47
 */
@Slf4j
public class TestServer {


    private static final  String DEFAULT_URL="F:\\";

    public void  run(final  int port,final  String url) throws Exception{
        NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(bossGroup, workerGroup)

                    .channel(NioServerSocketChannel.class)
                    // .handler(new TestServerInitializer()); 对应的bossGroup
                    // 对应是workerGroup
                    .childHandler(new TestServerInitializer());
            //sync 是将异步操作变成同步
            ChannelFuture channelFuture = bootstrap.bind("127.0.0.1",port).sync();
            log.info("服务段9999 端口启动成功");
            channelFuture.addListeners((ChannelFutureListener) future -> {
                if (future.isSuccess()) {
                    log.info("监听端口成功");
                } else {
                    log.info("监听端口失败");
                }
            });
            channelFuture.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {

        new TestServer().run(9999,DEFAULT_URL);

    }
}
